import cv2 as cv
import os

def __whichFilter(name):
  cur = os.path.dirname(os.path.abspath(__file__))
  fpath = ''
  f_list = ['huaijiu', 'taozi', 'riji', 'tiliang']

  if name not in f_list:
    raise ValueError('不包含该滤镜')

  if name == 'huaijiu':
    fpath = os.path.join(cur, 'material/f_huaijiu.jpg')
  elif name == 'taozi':  
    fpath = os.path.join(cur, 'material/f_peach.jpg')
  elif name == 'riji':
    fpath = os.path.join(cur, 'material/f_riji.jpg')
  elif name == 'tiliang':
    fpath = os.path.join(cur, 'material/f_tiliang.jpg')

  f = cv.imread(fpath)
  return f        

def filterImg(name, img):
  f = __whichFilter(name)
  h, w = img.shape[:2]

  for i in range(0, h):
    for j in range(0, w):
      (ibc, igc, irc) = img[i, j]
      x = int(irc / 4 + int((ibc%32) / 4) * 64)
      y = int(igc / 4 + int(ibc / 32) * 64)
      x = 511 if x >= 512 else x
      y = 511 if y >= 512 else y
      img[i, j] = f[y, x]

  return img    

